# coding=utf8

import EblobExecption
import Eblob.setting

from Eblob.HTable.ThriftConnection import ThriftConnection
from Eblob.HTable.Thrift2Connection import Thrift2Connection


class Connection:

    def __init__(self):
        self.thrift_connection = ThriftConnection()
        if Eblob.setting.protocol == 'thrift2':
            self.thrift_connection = Thrift2Connection()

    def is_existed(self, table_name, row_key, is_like=False):
        return self.thrift_connection.real_is_existed(table_name, row_key, is_like)

    def get_tables(self):
        return self.thrift_connection.get_tables()

    def connection_save(self, table_name, row_key, maps, ignore=False):
        if (not ignore) and self.thrift_connection.real_is_existed(table_name, row_key):
            raise EblobExecption.RowExistException('Error')
        self.thrift_connection.real_save(table_name, row_key, maps)

    def connection_update(self, table_name, row_key, maps):
        if self.thrift_connection.real_is_existed(table_name, row_key):
            self.thrift_connection.real_save(table_name, row_key, maps)
        else:
            raise EblobExecption.RowIsNotExistException('Error')

    def connection_find(self, table_name, row_key_like, start_row, counts):
        return self.thrift_connection.real_find(table_name, row_key_like, start_row, counts)

    def connection_get(self, table_name, row_key):
        return self.thrift_connection.real_get(table_name, row_key)

    def connection_delete(self, table_name, row_key):
        self.thrift_connection.real_delete(table_name, row_key)

    def connection_delete_like(self, table_name, row_key_like):
        self.thrift_connection.real_delete_like(table_name, row_key_like)
